using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

public class PlayerData : MonoBehaviour
{
	private static PlayerData _instance;

	public const string PLAYER_PREFS_NAME = "a";

	public const string PLAYER_PREFS_SKIN = "b";

	public const string PLAYER_PREFS_XP = "c";

	public const string PLAYER_PREFS_BEST_SCORE_SPEED_RUN = "d";

	public const string PLAYER_PREFS_BEST_SCORE_TIMER = "f";

	public const string PLAYER_PREFS_LAST_MODE = "e";

	public const string PLAYER_PREFS_UNLOCKED_SKINS = "g";

	public const string PLAYER_PREFS_SKIN_USE_COUNT = "h";

	public const string PLAYER_PREFS_VIBRATIONS = "i";

	public const string PLAYER_PREFS_LAST_CONNECTION = "j";

	public const string PLAYER_PREFS_GRAPHICS_SETTINGS = "k";

	public const string PLAYER_PREFS_UNLOCKED_MAPS = "l";

	public const string PLAYER_PREFS_LAST_MAP = "m";

	public const string PLAYER_PREFS_BEST_SCORE_MAPS = "d2";

	private string _pseudo = "Player";

	private int _skin;

	private int _xp;

	private float _bestScoreSpeedRun;

	private int _bestScoreTimer;

	private string _lastMode = "Timer";

	private string _skinUseCount = string.Empty;

	private List<string> _unlockedSkinsArray = new List<string>();

	private List<string> _unlockedMapArray = new List<string>();

	private bool _vibrations;

	private bool _lowGraphicSettings;

	private string _lastMap = string.Empty;

	private Dictionary<string, float> _bestScoreMaps = new Dictionary<string, float>();

	public string[] animalList = new string[12]
	{
		"Bear",
		"Cat",
		"Cow",
		"Deer",
		"Elephant",
		"Giraffe",
		"Horse",
		"Penguin",
		"Rabbit",
		"Snake",
		"Spider",
		"Wolf"
	};

	public static PlayerData Instance
	{
		get
		{
			if (_instance == null)
			{
				_instance = new GameObject("[PLAYER_DATA]").AddComponent<PlayerData>();
				UnityEngine.Object.DontDestroyOnLoad(_instance.gameObject);
				_instance.LoadData();
			}
			return _instance;
		}
	}

	public string Pseudo
	{
		get
		{
			return _pseudo;
		}
		set
		{
			_pseudo = value;
		}
	}

	public int Skin
	{
		get
		{
			return _skin;
		}
		set
		{
			_skin = value;
		}
	}

	public int Xp
	{
		get
		{
			return _xp;
		}
		set
		{
			_xp = value;
		}
	}

	public float BestScoreSpeedRun
	{
		get
		{
			return _bestScoreSpeedRun;
		}
		set
		{
			_bestScoreSpeedRun = value;
		}
	}

	public int BestScoreTimer
	{
		get
		{
			return _bestScoreTimer;
		}
		set
		{
			_bestScoreTimer = value;
		}
	}

	public string LastMode
	{
		get
		{
			return _lastMode;
		}
		set
		{
			_lastMode = value;
		}
	}

	public bool Vibrations
	{
		get
		{
			return _vibrations;
		}
		set
		{
			_vibrations = value;
		}
	}

	public bool LowGraphicSettings
	{
		get
		{
			return _lowGraphicSettings;
		}
		set
		{
			_lowGraphicSettings = value;
		}
	}

	public string LastMap
	{
		get
		{
			return _lastMap;
		}
		set
		{
			_lastMap = value;
		}
	}

	public void LoadData()
	{
		_pseudo = PlayerPrefs.GetString("a", "Player");
		_xp = PlayerPrefs.GetInt("c", 0);
		_bestScoreSpeedRun = PlayerPrefs.GetFloat("d", 0f);
		_bestScoreTimer = PlayerPrefs.GetInt("f", 0);
		_lastMode = PlayerPrefs.GetString("e", "Timer");
		_vibrations = PlayerPrefsX.GetBool("i", defaultValue: true);
		_lowGraphicSettings = PlayerPrefsX.GetBool("k", defaultValue: false);
		string @string = PlayerPrefs.GetString("g", string.Empty);
		string[] array = @string.Split(';');
		_unlockedSkinsArray.Clear();
		string[] array2 = array;
		foreach (string item in array2)
		{
			_unlockedSkinsArray.Add(item);
		}
		string string2 = PlayerPrefs.GetString("l", string.Empty);
		string[] array3 = string2.Split(';');
		_unlockedMapArray.Clear();
		string[] array4 = array3;
		foreach (string item2 in array4)
		{
			_unlockedMapArray.Add(item2);
		}
		_skin = PlayerPrefs.GetInt("b", 0);
		if (!IsSkinUnlocked(_skin))
		{
			_skin = 0;
		}
		_lastMap = PlayerPrefs.GetString("m", "City");
		if (!PlayerPrefs.HasKey("d2"))
		{
			GameModeParameters[] gameModes = GameModeManager.Instance.references.gameModes;
			foreach (GameModeParameters gameModeParameters in gameModes)
			{
				if (gameModeParameters.hasBestScore)
				{
					string key = string.Format("{0}{1}", "City", gameModeParameters.id);
					if (_bestScoreMaps.ContainsKey(key))
					{
						_bestScoreMaps[key] = _bestScoreTimer;
					}
					else
					{
						_bestScoreMaps.Add(key, _bestScoreTimer);
					}
				}
			}
		}
		else
		{
			string string3 = PlayerPrefs.GetString("d2", string.Empty);
			string[] array5 = string3.Split(';');
			string[] array6 = array5;
			foreach (string text in array6)
			{
				if (text.Contains("_"))
				{
					string[] array7 = text.Split('_');
					if (_bestScoreMaps.ContainsKey(array7[0]))
					{
						_bestScoreMaps[array7[0]] = float.Parse(array7[1]);
					}
					else
					{
						_bestScoreMaps.Add(array7[0], float.Parse(array7[1]));
					}
				}
			}
		}
		if (!PlayerPrefs.HasKey("j"))
		{
			PlayerPrefs.SetString("j", DateTime.UtcNow.ToString());
		}
	}

	public void SaveData()
	{
		PlayerPrefs.SetString("a", _pseudo);
		PlayerPrefs.SetInt("b", _skin);
		PlayerPrefs.SetInt("c", _xp);
		PlayerPrefs.SetFloat("d", _bestScoreSpeedRun);
		PlayerPrefs.SetInt("f", _bestScoreTimer);
		PlayerPrefs.SetString("e", _lastMode);
		PlayerPrefsX.SetBool("i", _vibrations);
		PlayerPrefsX.SetBool("k", _lowGraphicSettings);
		string value = string.Join(";", _unlockedSkinsArray.ToArray());
		PlayerPrefs.SetString("g", value);
		string value2 = string.Join(";", _unlockedMapArray.ToArray());
		PlayerPrefs.SetString("l", value2);
		PlayerPrefs.SetString("m", _lastMap);
		StringBuilder stringBuilder = new StringBuilder();
		foreach (KeyValuePair<string, float> bestScoreMap in _bestScoreMaps)
		{
			stringBuilder.AppendFormat("{0}_{1};", bestScoreMap.Key, bestScoreMap.Value);
		}
		PlayerPrefs.SetString("d2", stringBuilder.ToString());
		PlayerPrefs.Save();
	}

	public bool IsSkinUnlocked(int skinId)
	{
		if ((bool)DebugManager.Instance && DebugManager.Instance.TestModeEnabled)
		{
			return true;
		}
		return skinId < 13 || _unlockedSkinsArray.Contains(skinId.ToString());
	}

	public void SetSkinUnlocked(int skinId)
	{
		if (!IsSkinUnlocked(skinId))
		{
			_unlockedSkinsArray.Add(skinId.ToString());
		}
		SaveData();
	}

	public void RegisterSkinUse(int index)
	{
		string key = string.Format("{0}{1}", "h", index);
		int @int = PlayerPrefs.GetInt(key, 0);
		PlayerPrefs.SetInt(key, @int + 1);
		PlayerPrefs.Save();
	}

	public bool HasUsedSkin(int index)
	{
		string key = string.Format("{0}{1}", "h", index);
		return PlayerPrefs.HasKey(key);
	}

	public void SetBestScore(string mode, float score)
	{
		string key = $"City{mode}";
		if (_bestScoreMaps.ContainsKey(key))
		{
			_bestScoreMaps[key] = score;
		}
		else
		{
			_bestScoreMaps.Add(key, score);
		}
	}

	public float GetBestScore(string mode)
	{
		string key = $"City{mode}";
		if (_bestScoreMaps.ContainsKey(key))
		{
			return _bestScoreMaps[key];
		}
		return 0f;
	}

	public DateTime GetLastTime()
	{
		try
		{
			return DateTime.Parse(PlayerPrefs.GetString("j", DateTime.UtcNow.ToString()));
		}
		catch (FormatException)
		{
			PlayerPrefs.SetString("j", DateTime.UtcNow.ToString());
			return DateTime.UtcNow;
		}
	}

	public void SetLastTimeAsNow()
	{
		PlayerPrefs.SetString("j", DateTime.UtcNow.ToString());
	}

	public bool HasCapturedAnimal(string animalId)
	{
		return PlayerPrefs.GetInt($"Ach_{animalId}", 0) > 0;
	}

	public void AddCapturedAnimal(string animalId)
	{
		int @int = PlayerPrefs.GetInt($"Ach_{animalId}", 0);
		PlayerPrefs.SetInt($"Ach_{animalId}", @int + 1);
		PlayerPrefs.Save();
	}

	public int GetCapturedAnimalsCount()
	{
		int num = 0;
		string[] array = animalList;
		foreach (string animalId in array)
		{
			if (HasCapturedAnimal(animalId))
			{
				num++;
			}
		}
		return num;
	}
}
